/**
 * 
 */
package com.ubo.visu.painter.node;

import java.util.ArrayList;
import java.util.List;

import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineBuilder;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;

import com.ubo.visu.model.EEntityType;
import com.ubo.visu.model.MemberAttribute;
import com.ubo.visu.model.MemberMethod;
import com.ubo.visu.model.ModelNode;
import com.ubo.visu.painter.UtilsSize;
import com.ubo.visu.util.FxUtil;
import com.ubo.visu.util.PropertiesManager;

/**
 * Permet de dessiner un rectangle.
 * 
 * @author saludegu
 * 
 */
public class RectangleNodePainter extends ANodePainter {

	/**
	 * 
	 * Constructor
	 * 
	 */
	public RectangleNodePainter() {
		super();
	}

	/**
	 * @see com.ubo.visu.painter.node.INodePainter#paint(com.ubo.visu.model.ModelNode)
	 */
	@Override
	public void paint(ModelNode node) {
		int curLayoutY = 0;
		final List<Node> nodeList = new ArrayList<Node>();
		final Rectangle newRectangle = RectangleBuilder
				.create()
				.width(UtilsSize.computeRectangleWidth(node))
				.height(UtilsSize.computeRectangleHeight(node))
				.fill(Color.web(PropertiesManager.getInstance().getProperty(
						PROPERTY_NODE_COLOR)))
				.stroke(Color.web(PropertiesManager.getInstance().getProperty(
						PROPERTY_NODE_STROKE_COLOR))).build();
		nodeList.add(newRectangle);
		if (node.getEntityType().equals(EEntityType.INTERFACE)) {
			Text newTextInterface = TextBuilder
					.create()
					.layoutY(15)
					.text("<<Interface>>")
					.fill(Color.web(PropertiesManager.getInstance()
							.getProperty(PROPERTY_NODE_TEXT_COLOR))).build();
			nodeList.add(newTextInterface);
			curLayoutY += 15;
		}
		curLayoutY += 15;
		final Text newText = TextBuilder
				.create()
				.layoutY(curLayoutY)
				.text(node.getNameNode())
				.fill(Color.web(PropertiesManager.getInstance().getProperty(
						PROPERTY_NODE_TEXT_COLOR))).build();
		nodeList.add(newText);
		Line newLine = LineBuilder
				.create()
				.fill(Color.web(PropertiesManager.getInstance().getProperty(
						PROPERTY_NODE_COLOR)))
				.stroke(Color.web(PropertiesManager.getInstance().getProperty(
						PROPERTY_NODE_STROKE_COLOR))).startY(curLayoutY + 5)
				.endY(curLayoutY + 5).startX(0).endX(newRectangle.getWidth())
				.build();
		nodeList.add(newLine);
		// Generation des attributs.
		final List<Text> mMemberList = new ArrayList<Text>();
		if (node.getAttributes() != null) {
			for (MemberAttribute attr : node.getAttributes()) {
				Text newAttrText = TextBuilder
						.create()
						.text(attr.toString())
						.fill(Color.web(PropertiesManager.getInstance()
								.getProperty(PROPERTY_NODE_TEXT_COLOR)))
						.layoutY(20 * mMemberList.size() + curLayoutY + 15)
						.build();
				mMemberList.add(newAttrText);
			}
		}
		// G�n�ration des m�thodes
		if (node.getMethodes() != null) {
			int yPos = 20;
			if (node.getAttributes() != null)
				yPos = 25 + 20 * node.getAttributes().size();
			newLine = LineBuilder
					.create()
					.fill(Color.web(PropertiesManager.getInstance()
							.getProperty(PROPERTY_NODE_COLOR)))
					.stroke(Color.web(PropertiesManager.getInstance()
							.getProperty(PROPERTY_NODE_STROKE_COLOR)))
					.startY(yPos).endY(yPos).startX(0)
					.endX(newRectangle.getWidth()).build();
			nodeList.add(newLine);
			for (MemberMethod attr : node.getMethodes()) {
				Text newAttrText = TextBuilder
						.create()
						.text(attr.toString())
						.fill(Color.web(PropertiesManager.getInstance()
								.getProperty(PROPERTY_NODE_TEXT_COLOR)))
						.layoutY(20 * mMemberList.size() + curLayoutY + 20)
						.build();
				mMemberList.add(newAttrText);
			}
		}
		FxUtil.runOnFx(new Runnable() {

			@Override
			public void run() {
				mContainer.getChildren().addAll(nodeList);
				mContainer.getChildren().addAll(mMemberList);
			}
		});
	}
}
